package com.xiaolin.algorithm.string;

import java.util.Arrays;

/**
 * @ClassName Num
 * @Description 获取数字
 * @Detail detail
 * @Author MyPC
 * @Date 2020/12/28
 * @Version 1.0
 */
public class Num {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(strToInt("200000000000"));
        System.out.println(reverseLeftWords("hjdghjdg",2));
        System.out.println(reverseWords("a good   example"));
    }
    public static int strToInt(String str) {
        char[] chars = str.toCharArray();
        StringBuilder sb=new StringBuilder();
        for (char aChar : chars) {
            if(sb.length()==0){
                if(aChar==' '){

                }else if(aChar=='-' || aChar=='+' || (aChar>='0' && aChar <='9')){
                    sb.append(aChar);
                }else {
                    break;
                }
            }else {
                if(aChar<'0' || aChar >'9'){
                    break;
                }else {
                    sb.append(aChar);
                }
            }
        }
        if("".equals(sb.toString()) || "-".equals(sb.toString()) || "+".equals(sb.toString())){
            return 0;
        }else{
            String result="";
            if(sb.toString().length()>11){
                result=sb.substring(0,12);
            }
            long sum = Long.parseLong(result);
            if(sum > Integer.MAX_VALUE || sum<Integer.MIN_VALUE){
                return sum>0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            }
        }
        return Integer.parseInt(sb.toString());
    }

    public static String reverseLeftWords(String s, int n) {
        //第一种解法
//        StringBuilder sb=new StringBuilder();
//        for(int i=0;i<n;i++){
//            sb.append(s.charAt(i));
//        }
//        return s.substring(n, s.length())+sb.toString();
        // 第二种解法
        return s.substring(n,s.length())+s.substring(0,n);
    }

    public static String reverseWords(String s) {
        StringBuilder sb=new StringBuilder();
        String[] split = s.split(" ");
        for(int i=split.length-1;i>=0;i--){
            if(!split[i].equals("")) sb.append(split[i]).append(" ");
        }
        sb.replace(sb.length()-1,sb.length(),"");
        return sb.toString();
    }

    public int search(int[] nums, int target) {
        int count=0;
        for (int num : nums) {
            if(num==target)count++;
        }
        return count;
    }

    public int missingNumber(int[] nums) {
        if(nums[nums.length-1]==nums.length-1){
            return nums.length;
        }
        if(nums[0]>0){
            return 0;
        }
        int index=0;
        for(int i=0;i<nums.length-1;i++){
            if(nums[i+1]-nums[i]>1)index=i;
        }
        return nums[index]+1;
    }

    public int reversePairs(int[] nums) {
        int count=0;
        for(int i=0;i<nums.length-1;i++){
            int pre=nums[i];
            for(int j=i+1;j<nums.length;j++){
                if(pre>nums[j]){
                    count++;
                }
            }
        }
        return count;
    }

    public int[] getLeastNumbers(int[] arr, int k) {
        Arrays.sort(arr);
        int[] result=new int[k];
        for (int i=0;i<k;i++){
            result[i]=arr[i];
        }
        return result;
    }


    public int minArray(int[] numbers) {
        int result=numbers[0];
        int count=0;
        for(int i=1;i<numbers.length;i++){
            if(result<=numbers[i]){
                result=numbers[i];
                count++;
            }else {
                result=numbers[i];
                break;
            }
        }
        // 一直递增，直到数组末尾
        if(count==numbers.length-1){
            return numbers[0];
        }
        return result;
    }
}
